// pages/acceptSearch/acceptSearch.js
import api from "../../config/api";
import request from "../../utils/request";
import { shareOptions } from "../../utils/access";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    isShow:'1',
    inputShowed: false,
    inputVal: "",
    // 下拉菜单的数据
    countries: [],
    countryIndex: 0,
    // 搜索界面的数据
    searchData:[],
    // 输入框中的值
    value:'',
    // 空间列表数据
    spanceData:[],
    // 当前页码值
    current:1,
    // 页码总数
    pages:0,
    isLoaing:false,
    // 搜索条件
    spanceListId:'',
    // 输入框的焦点事件
    inputShowed: false,
  },
  // 获得下拉菜单列表
  async getSearchData(){
    const { data } = await request.get(api.spaceList)
    const a = ['全部']
    data.spaceData?.map((item)=>{
      a?.push(item.spaceName)
    })
    console.log(a)
    this.setData({
      spanceData:data.spaceData,
      countries:a
    })
  },
  // 获取物品列表
  async getGoodsData(current){
    current = this.data.current
    console.log(this.data.spanceListId,'空间ID')
    const { data } = await request.get(api.getUserGoods,{
      itemFullName:this.data.value,
      spaceId:this.data.spanceListId,
      page:this.data.current,
      size:10
    },{loading:false})
    console.log(data)
      // 分页的数据列表
    this.setData({
      searchData: this.data.current === 1 ? (data.records || []) : [
        ...this.data.searchData,
        ...data.records,
      ],
      total:data.total,
      current:data.current,
      pages:data.pages
    })
    // 结束下拉刷新
    wx.stopPullDownRefresh()
  },
  // 下拉菜单值改变触发的事件
  bindCountryChange: function(e) {
    const { spanceData, countries } = this.data
    console.log(e.detail.value);
    this.setData({
      current:1,
      isLoaing:false,
      countryIndex: e.detail.value
    })
    if (e.detail.value === '0'){
      this.setData({
        current:1,
        isLoaing:false,
        spanceListId:''
      })
      console.log("发送全部请求")
      return this.data.value === '' ? console.log(1) : this.getGoods()
    }
    console.log(spanceData)
    console.log(countries[e.detail.value])
    // 找到相同的空间名，返回这组对象的id
    const sendId =  spanceData.filter(item=>{
      return item.spaceName === countries[e.detail.value]
    })
    console.log(sendId[0].id)
    this.setData({
      spanceListId:sendId[0].id
    })

    console.log('==================================', this.data.spanceListId)
    if (this.data.value === ''){
      return
    }else{
      this.getGoods(sendId[0].id)
    }
    console.log('下拉菜单改变')
},
  // 发送搜索请求
  async getGoods(id){
    console.log(id)
    const {data} =  await request.get(api.getUserGoods,{
      spaceId:id,
      itemFullName:this.data.value,
      page:this.data.current,
      size:10
    })
    // if (id === undefined) {
    // }
    this.setData({
      // searchData:data.records,
      searchData: this.data.current === 1 ? (data.records || []) : [
        ...this.data.searchData,
        ...data.records
      ],
    })
  },
  
  // 跳转到物品详情页面
  goAcceptDetails(e){
    console.log(e)
    wx.navigateTo({
      url: `/pages/acceptDetails/acceptDetails?id=${e.currentTarget.dataset.id}`,
    })
  },
  // 搜索输入框改变事件
  searchInput(e){
    console.log(e.detail.value)
    if (e.detail.value === ''){
      return this.setData({
        searchData:''
      })
    }
    this.setData({
      value:e.detail.value,
      isLoaing:false
    })
    this.getGoodsData()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.getGoodsData()
    this.setData({
      inputShowed: true
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      current:1,
      isLoaing:false
    })
    this.getSearchData()
    // this.getGoodsData()
    // this.getGoods()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    console.log(this.data.spanceListId)
    this.setData({
      current:1,
      isLoaing:false, 
    })
    this.getGoodsData(1)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log('1')
    let { current, pages } = this.data
    if (current < pages){
      this.setData({
        current:this.data.current + 1,
      })
      this.getGoodsData(current)
    }else{
      this.setData({
        isLoaing:true,
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage(e) {
		return shareOptions('default',e)
	},
})